; demo.asm
; controls demo window
;
	.include "common.inc"
	.include "global.inc"
	.include "window.inc"
	.include "execfun.inc"
	.include "cursor.inc"
	.include "wutil.inc"

	.export demo

	.segment "RODATA"
	
demo_title:	.byte 11, $44, $05, $0D, $0F, $20, $57, $09, $0E, $04, $0F, $17

tx_plus:	.byte 1, $2B
tx_minus:	.byte 1, $2D
tx_input:	.byte 6, $49, $0E, $10, $15, $14, $3A
tx_close:	.byte 5, $43, $0C, $0F, $13, $05

b_mue:		.byte $48, $48, $48, $48, $48, $78, $40, $40 
bm_mue:		.byte 8, 8
			.word b_mue
tx_mue:		.byte $FF
			.word bm_mue

	.segment "DATA"

tx_number:	.byte 4, $30, $30, $30, $30
tx_edit:	.byte 11, $45, $04, $09, $14, $20, $0D, $05, $20, $20, $20, $20

f_demo:		.byte $00
h_demo:		.byte $00
h_number:	.byte $00

	.segment "CODE"

demo:		lda f_demo
			beq @create
			jmp frm_activate
@create:	; create main frame
			ldx #MAX_WIDTH/2
			ldy #MAX_HEIGHT/3
			jsr frm_create
			sta f_demo
			ldx #MAX_WIDTH/4
			ldy #MAX_HEIGHT/3+16
			jsr frm_setpos
			jsr frm_getwin
			sta h_demo
			jsr win_realize
			lda #WSTYLE_BCKGND+WSTYLE_HASTITLE
			jsr win_setstyle
			lda #1					; lower case system font
			jsr win_setfont
			lda #<demo_title
			ldx #>demo_title
			jsr win_settext
			
			; create minus button			
			lda h_demo
			jsr win_create
			lda #WSTYLE_BORDER
			jsr win_setstyle
			ldx #8
			ldy #20
			jsr win_setpos
			ldx #14
			ldy #12
			jsr win_setdim
			lda #<tx_minus
			ldx #>tx_minus
			jsr win_settext
			lda #WTYPE_BUTTON
			jsr win_settype
			lda #<ex_minus_button
			ldx #>ex_minus_button
			jsr win_setexec

			; create number window
			lda h_demo
			jsr win_create
			sta h_number
			lda #WSTYLE_BCKGND
			jsr win_setstyle
			ldx #32 
			ldy #20
			jsr win_setpos
			ldx #40
			ldy #12
			jsr win_setdim
			lda #<tx_number
			ldx #>tx_number
			jsr win_settext

			; create plus button			
			lda h_demo
			jsr win_create
			lda #WSTYLE_BORDER
			jsr win_setstyle
			ldx #76
			ldy #20
			jsr win_setpos
			ldx #14
			ldy #12
			jsr win_setdim
			lda #<tx_plus
			ldx #>tx_plus
			jsr win_settext
			lda #WTYPE_BUTTON
			jsr win_settype
			lda #<ex_plus_button
			ldx #>ex_plus_button
			jsr win_setexec

			; create label
			lda h_demo
			jsr win_create
			lda #WSTYLE_TXLEFT
			jsr win_setstyle
			ldx #6
			ldy #40
			jsr win_setpos
			ldx #60
			ldy #12
			jsr win_setdim
			lda #<tx_input
			ldx #>tx_input
			jsr win_settext

			; create input field
			lda h_demo
			jsr win_create
			lda #WSTYLE_TXRIGHT|WSTYLE_BCKGND
			jsr win_setstyle
			ldx #8 
			ldy #55
			jsr win_setpos
			ldx #88
			ldy #12
			jsr win_setdim
			lda #<tx_edit
			ldx #>tx_edit
			jsr win_settext
			lda #WTYPE_EDIT
			jsr win_settype
;			lda #<ex_input_field
;			ldx #>ex_input_field
;			jsr win_setexec
			
			; create close button
			lda h_demo
			jsr win_create
			lda #WSTYLE_BORDER
			jsr win_setstyle
			ldx #30
			ldy #75
			jsr win_setpos
			ldx #44
			ldy #12
			jsr win_setdim
			lda #<tx_close
			ldx #>tx_close
			jsr win_settext
			lda #WTYPE_BUTTON
			jsr win_settype
			lda #<ex_close_button
			ldx #>ex_close_button
			jsr win_setexec
			
			; create sig
			lda h_demo
			jsr win_create
			ldx #96 
			ldy #84
			jsr win_setpos
			ldx #8
			ldy #8
			jsr win_setdim
			lda #<tx_mue
			ldx #>tx_mue
			jsr win_settext

			; draw window			
			lda h_demo
			jsr win_draw
			lda h_demo
			jsr win_show			; to screen

			rts

ex_plus_button:
			jsr btncheck
			ldx #4
@inclp:		inc tx_number, x
			lda #$39
			cmp tx_number, x
			bcs @novl
			lda #$30
			sta tx_number, x
			dex
			bne @inclp
@novl:		lda h_number
			jsr win_draw
			rts

ex_minus_button:
			jsr btncheck
			ldx #4
@declp:		dec tx_number, x
			lda #$2F
			cmp tx_number, x
			bcc @noufl
			lda #$39
			sta tx_number, x
			dex
			bne @declp
@noufl:		lda h_number
			jsr win_draw
			rts

ex_close_button:
			jsr btncheck
			lda f_demo
			jsr frm_destroy
			lda #0
			sta f_demo
			rts

;ex_input_field:
;			jmp ex_edit
